###Clear R--------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation---------------------
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("psych", "ggplot2", "interplot", "MASS", "foreign",  "car", "stringr", "stargazer", "xtable", "moments", "dplyr", "lavaan", "interplot")
ipak(packages)

#load data
load("Study5_TTIP_data.RData")


### Appendix H.1 Design------------

data <- subset(data,is.na(partyidentity)==F) #only keep the partisans
table(data$pid)

summary(data$partyidentity)
sd(data$partyidentity)
data$huddy1 <- ifelse(!is.na(data$huddyid_dem_1),data$huddyid_dem_1,data$Q11_1)
data$huddy2 <- ifelse(!is.na(data$huddyid_dem_2),data$huddyid_dem_2,data$Q11_2)
data$huddy3 <- ifelse(!is.na(data$huddyid_dem_3),data$huddyid_dem_3,data$Q11_3)
data$huddy4 <- ifelse(!is.na(data$huddyid_dem_4),data$huddyid_dem_4,data$Q11_4)
data$huddy5 <- ifelse(!is.na(data$huddyid_dem_5),data$huddyid_dem_5,data$Q11_5)
data$huddy6 <- ifelse(!is.na(data$huddyid_dem_6),data$huddyid_dem_6,data$Q11_6)
data$huddy7 <- ifelse(!is.na(data$huddyid_dem_7),data$huddyid_dem_7,data$Q11_7)
data$huddy8 <- ifelse(!is.na(data$huddyid_dem_8),data$huddyid_dem_8,data$Q11_8)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T)

mean(zero1(data$CRTall))
sd(zero1(data$CRTall))
psych::alpha(with(data,data.frame(data$CRT1,data$CRT2, data$CRT3)))

mean(data$nfc)
sd(data$nfc)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(NfC_1,NfC_2,NfC_3,NfC_4,NfC_5,NfC_6,NfC_7,NfC_8,NfC_9,NfC_10,NfC_11,NfC_12,NfC_13,NfC_14,NfC_15,NfC_16,NfC_17,NfC_18))),check.keys = T)

##Latent cognitive resources variable

mean(data$cogresources)
sd(data$cogresources)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1, CRT2, CRT3, NfC_1,NfC_2,NfC_3,NfC_4,NfC_5,NfC_6,NfC_7,NfC_8,NfC_9,NfC_10,NfC_11,NfC_12,NfC_13,NfC_14,NfC_15,NfC_16,NfC_17,NfC_18))),check.keys = T)

cor.test(data$CRTall, data$nfc)
cor.test(data$CRTall, data$cogresources)
cor.test(data$nfc, data$cogresources)

cor.test(data$partyidentity, data$CRTall)
cor.test(data$partyidentity, data$nfc)
cor.test(data$partyidentity, data$cogresources)

summary(data$supportttip)
sd(data$supportttip)

### Appendix H.2 Results --- Main effects -----------------
main<-(lm(supportttip~ownpartysupport+partyidentity+CRTall+simplecomplex+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))
main1<-(lm(supportttip~ownpartysupport+partyidentity+nfc+simplecomplex+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))
main2<-(lm(supportttip~ownpartysupport+partyidentity+cogresources+simplecomplex+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))

stargazer(main, main1, main2, title="Trade Policy Experiment: Main Effect of Party Cues on TTIP Support", align=TRUE, order = c(1,6, 3, 4, 5, 2,7,8,9,10, 11), covariate.labels=c("In-Party cue", "Complex frame", "CRT", "NfC", "Cognitive resources", "PSID Strength", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican"), omit.stat=c("LL","ser","f", "adj.rsq"), notes.append = FALSE, star.cutoffs=c(0.05), notes = "*p<0.05",  no.space=TRUE,out = "Tablesmain.tex",dep.var.caption = "Policy support",dep.var.labels.include = F,label="tab:ttipmain", digits=2)

### Appendix H.2 Results --- Interaction effects -----------------
twoway_crt<-(lm(supportttip~ownpartysupport+partyidentity+CRTall+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*CRTall+partyidentity*CRTall+ownpartysupport*simplecomplex + simplecomplex*partyidentity + simplecomplex*CRTall+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))
twoway_nfc<-(lm(supportttip~ownpartysupport+partyidentity+nfc+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*nfc+partyidentity*nfc+ownpartysupport*simplecomplex +  simplecomplex*partyidentity + simplecomplex*nfc+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))
twoway_cog<-(lm(supportttip~ownpartysupport+partyidentity+cogresources+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*cogresources+partyidentity*cogresources+ownpartysupport*simplecomplex +  simplecomplex*partyidentity + simplecomplex*cogresources+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))

full_crt <- lm(supportttip~ownpartysupport+partyidentity+CRTall+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*CRTall+partyidentity*CRTall+ownpartysupport*simplecomplex+  + simplecomplex*partyidentity + simplecomplex*CRTall +  CRTall*partyidentity*ownpartysupport+age+female+non_white+as.factor(education)+Republican_dummy,data)
full_nfc <- lm(supportttip~ownpartysupport+partyidentity+nfc+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*nfc+partyidentity*nfc+ownpartysupport*simplecomplex+  + simplecomplex*partyidentity + simplecomplex*nfc+nfc*partyidentity*ownpartysupport+age+female+non_white+as.factor(education)+Republican_dummy,data)
full_cog <- lm(supportttip~ownpartysupport+partyidentity+cogresources+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*cogresources+partyidentity*cogresources+ownpartysupport*simplecomplex+  + simplecomplex*partyidentity + simplecomplex*cogresources +  cogresources*partyidentity*ownpartysupport+age+female+non_white+as.factor(education)+Republican_dummy,data)

#replace names 2 way model
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc"] <- "CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "ownpartysupport1:nfc"] <- "ownpartysupport1:CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "partyidentity:nfc"] <- "partyidentity:CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources"] <- "CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "ownpartysupport1:cogresources"] <- "ownpartysupport1:CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "partyidentity:cogresources"] <- "partyidentity:CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

# replace names full model
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "nfc"] <- "CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "ownpartysupport1:nfc"] <- "ownpartysupport1:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "partyidentity:nfc"] <- "partyidentity:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "ownpartysupport1:nfc"] <- "ownpartysupport1:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "ownpartysupport1:partyidentity:nfc"] <- "ownpartysupport1:partyidentity:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "nfc:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

names(full_cog$coefficients)[names(full_cog$coefficients) == "cogresources"] <- "CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "ownpartysupport1:cogresources"] <- "ownpartysupport1:CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "partyidentity:cogresources"] <- "partyidentity:CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "cogresources:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"
names(full_cog$coefficients)[names(full_cog$coefficients) == "ownpartysupport1:partyidentity:cogresources"] <- "ownpartysupport1:partyidentity:CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "cogresources:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

stargazer(twoway_crt, full_crt, twoway_nfc, full_nfc,twoway_cog, full_cog, title="Trade Policy Experiment: Policy support, party cues, cognitive resources and social identity strength", align=TRUE, order = c(1, 4, 2, 3, 12, 11, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10),covariate.labels=c("In-party cue", "Complexity", "Partisan Social Identity Strength (PID)", "Cognitive resources", "In-party * Cog", "In-party * PSID", "PSID * Cog", "In-party * Complexity", "PSID * Complexity", "Cog * Complexity", "In-party * PSID * Cog", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican"), omit.stat=c("LL","ser","f", "adj.rsq"), notes.append = FALSE, star.cutoffs=c(0.05), notes = "*p<0.05", column.sep.width = "1pt", no.space=TRUE, font.size="tiny" ,out = "Study5_TTIP_full_interaction.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Cog resources"), column.separate = c(2,2,2), dep.var.labels.include = F,label="tab:ttipfull", digits=2)

## CRT at 0 or -1SD
m0 <- lm(supportttip~ownpartysupport*scale(CRTall,center=0,scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_0 <- interplot(m0,var1 = "ownpartysupport",var2='partyidentity')
m_0$data$CRT='-1 SD'
## CRT at mean
m1 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1 <- interplot(m2,var1 = "ownpartysupport",var2='partyidentity')
m_1$data$CRT='Mean'
## CRT at +1
m2 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2 <- interplot(m2,var1 = "ownpartysupport",var2='partyidentity')
m_2$data$CRT='+1 SD'
forplot <- rbind(m_0$data,m_1$data,m_2$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

## NFC at -1SD
m0_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m2_nfc)
m_0_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_0_nfc$data$CRT='-1 SD'
## NFC at mean
m1_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_1_nfc$data$CRT='Mean'
## CRT at +1
m2_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_2_nfc$data$CRT='+1 SD'

forplot_nfc <- rbind(m_0_nfc$data,m_1_nfc$data,m_2_nfc$data)
forplot_nfc$battery      <- c("NFC")

## Cog at -1SD
m0_nfc <- lm(supportttip~ownpartysupport*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_0_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_0_nfc$data$CRT='-1 SD'
## Cog at mean
m1_nfc <- lm(supportttip~ownpartysupport*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_1_nfc$data$CRT='Mean'
## Cog at +1
m2_nfc <- lm(supportttip~ownpartysupport*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_2_nfc$data$CRT='+1 SD'

forplot_cog <- rbind(m_0_nfc$data,m_1_nfc$data,m_2_nfc$data)
forplot_cog$battery      <- c("Cognitive\n resources")

#combine CRT and NFC results
forplot_comb <- rbind(forplot, forplot_nfc, forplot_cog)

forplot_comb$CRT <- factor(forplot_comb$CRT,levels = c("-1 SD","Mean","+1 SD"))
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NFC","Cognitive\n resources"))

ggplot(forplot_comb,aes(x=fake,y=coef1))+geom_line()+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of In-party Cue on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub),alpha=.5, fill='darkgreen', colour="black")+geom_hline(yintercept = 0,lty="dashed") + theme(strip.text.y = element_text(angle = 360))

#save results
ggsave("Study5_TTIP_CRT_NFC.pdf",width=8,height=6)

### Appendix H.3 Results conditional upon simple and complex cues--------------

twoway_crt<-(lm(supportttip~ownpartysupport+partyidentity+CRTall+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*CRTall+partyidentity*CRTall+ownpartysupport*simplecomplex + simplecomplex*partyidentity + simplecomplex*CRTall+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))
twoway_nfc<-(lm(supportttip~ownpartysupport+partyidentity+nfc+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*nfc+partyidentity*nfc+ownpartysupport*simplecomplex +  simplecomplex*partyidentity + simplecomplex*nfc+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))
twoway_cog<-(lm(supportttip~ownpartysupport+partyidentity+cogresources+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*cogresources+partyidentity*cogresources+ownpartysupport*simplecomplex +  simplecomplex*partyidentity + simplecomplex*cogresources+age+female+non_white+as.factor(education)+Republican_dummy,subset(data)))

full_crt <- lm(supportttip~ownpartysupport+partyidentity+CRTall+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*CRTall+partyidentity*CRTall+ownpartysupport*simplecomplex+  + simplecomplex*partyidentity + simplecomplex*CRTall +  CRTall*partyidentity*ownpartysupport+age+female+non_white+as.factor(education)+Republican_dummy,data)
full_nfc <- lm(supportttip~ownpartysupport+partyidentity+nfc+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*nfc+partyidentity*nfc+ownpartysupport*simplecomplex+  + simplecomplex*partyidentity + simplecomplex*nfc+nfc*partyidentity*ownpartysupport+age+female+non_white+as.factor(education)+Republican_dummy,data)
full_cog <- lm(supportttip~ownpartysupport+partyidentity+cogresources+simplecomplex+ownpartysupport*partyidentity+ownpartysupport*cogresources+partyidentity*cogresources+ownpartysupport*simplecomplex+  + simplecomplex*partyidentity + simplecomplex*cogresources +  cogresources*partyidentity*ownpartysupport+age+female+non_white+as.factor(education)+Republican_dummy,data)

#replace names 2 way model
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc"] <- "CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "ownpartysupport1:nfc"] <- "ownpartysupport1:CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "partyidentity:nfc"] <- "partyidentity:CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources"] <- "CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "ownpartysupport1:cogresources"] <- "ownpartysupport1:CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "partyidentity:cogresources"] <- "partyidentity:CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

# replace names full model
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "nfc"] <- "CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "ownpartysupport1:nfc"] <- "ownpartysupport1:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "partyidentity:nfc"] <- "partyidentity:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "ownpartysupport1:nfc"] <- "ownpartysupport1:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "ownpartysupport1:partyidentity:nfc"] <- "ownpartysupport1:partyidentity:CRTall"
names(full_nfc$coefficients)[names(full_nfc$coefficients) == "nfc:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

names(full_cog$coefficients)[names(full_cog$coefficients) == "cogresources"] <- "CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "ownpartysupport1:cogresources"] <- "ownpartysupport1:CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "partyidentity:cogresources"] <- "partyidentity:CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "cogresources:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"
names(full_cog$coefficients)[names(full_cog$coefficients) == "ownpartysupport1:partyidentity:cogresources"] <- "ownpartysupport1:partyidentity:CRTall"
names(full_cog$coefficients)[names(full_cog$coefficients) == "cogresources:simplecomplexSimple"] <- "CRTall:simplecomplexSimple"

stargazer(twoway_crt, full_crt, twoway_nfc, full_nfc,twoway_cog, full_cog, title="Trade Policy Experiment: Policy support, party cues, cognitive resources and social identity strength", align=TRUE, order = c(1, 4, 2, 3, 12, 11, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10),covariate.labels=c("In-party cue", "Complexity", "Partisan Social Identity Strength (PID)", "Cognitive resources", "In-party * Cog", "In-party * PSID", "PSID * Cog", "In-party * Complexity", "PSID * Complexity", "Cog * Complexity", "In-party * PSID * Cog", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican"), omit.stat=c("LL","ser","f", "adj.rsq"), notes.append = FALSE, star.cutoffs=c(0.05), notes = "*p<0.05", column.sep.width = "1pt", no.space=TRUE, font.size="tiny" ,out = "Study1_full_interaction.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Cog resources"), column.separate = c(2,2,2), dep.var.labels.include = F,label="tab:ttipfull", digits=2)

### Appendix H.3 Results conditional upon simple and complex cues --- Figure for complex cues--------------
data$simlecomplex_dum<-as.numeric(data$simplecomplex)

## CRT at 0 or -1SD
m0 <- lm(supportttip~ownpartysupport*scale(CRTall,center=0,scale=F)*partyidentity, data=subset(data,simlecomplex_dum==1))
summary(m0)
m_0 <- interplot(m0,var1 = "ownpartysupport",var2='partyidentity')
m_0$data$CRT='-1 SD'

## CRT at mean
m1 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==1))
summary(m1)
m_1 <- interplot(m1,var1 = "ownpartysupport",var2='partyidentity')
m_1$data$CRT='Mean'

## CRT at +1
m2 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==1))
summary(m2)
m_2 <- interplot(m2,var1 = "ownpartysupport",var2='partyidentity')
m_2$data$CRT='+1 SD'

forplot <- rbind(m_0$data,m_1$data,m_2$data)
forplot$battery      <- c("CRT")

forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

## NFC at -1SD
m0_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==1))
summary(m0_nfc)
m_0_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_0_nfc$data$CRT='-1 SD'

## CRT at mean
m1_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity,data=subset(data,simlecomplex_dum==1))
m_1_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_1_nfc$data$CRT='Mean'

## CRT at +1
m2_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==1))
summary(m2_nfc)
m_2_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_2_nfc$data$CRT='+1 SD'

forplot_nfc <- rbind(m_0_nfc$data,m_1_nfc$data,m_2_nfc$data)
forplot_nfc$battery      <- c("NfC")

#combine CRT and NFC results
forplot_comb <- rbind(forplot, forplot_nfc)

forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))
ggplot(forplot_comb,aes(x=fake,y=coef1))+geom_line()+facet_grid(battery~CRT)+xlab("Party ID Strength")+theme_bw()+ylab("Marginal effect of In-party Cue on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub),alpha=.5, fill='darkgreen', colour="black")+geom_hline(yintercept = 0,lty="dashed")

#save results
ggsave("TTIP_CRT_NFC_complex.pdf",width=8,height=3)

### Appendix H.3 Results conditional upon simple and complex cues --- Figure for simple cues--------------

## CRT at 0 or -1SD
m0 <- lm(supportttip~ownpartysupport*scale(CRTall,center=0,scale=F)*partyidentity, data=subset(data,simlecomplex_dum==2))
summary(m0)
m_0 <- interplot(m0,var1 = "ownpartysupport",var2='partyidentity')
m_0$data$CRT='-1 SD'

## CRT at mean
m1 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==2))
summary(m1)
m_1 <- interplot(m1,var1 = "ownpartysupport",var2='partyidentity')
m_1$data$CRT='Mean'

## CRT at +1
m2 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==2))
summary(m2)
m_2 <- interplot(m2,var1 = "ownpartysupport",var2='partyidentity')
m_2$data$CRT='+1 SD'

forplot <- rbind(m_0$data,m_1$data,m_2$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

## NFC at -1SD
m0_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==2))
summary(m0_nfc)
m_0_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_0_nfc$data$CRT='-1 SD'

## CRT at mean
m1_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity,data=subset(data,simlecomplex_dum==2))
m_1_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_1_nfc$data$CRT='Mean'

## CRT at +1
m2_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity, data=subset(data,simlecomplex_dum==2))
summary(m2_nfc)
m_2_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity')
m_2_nfc$data$CRT='+1 SD'

forplot_nfc <- rbind(m_0_nfc$data,m_1_nfc$data,m_2_nfc$data)
forplot_nfc$battery      <- c("NfC")

#combine CRT and NFC results
forplot_comb <- rbind(forplot, forplot_nfc)

forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))
#forplot$condition <- car::recode(as.numeric(forplot$value),"1='In-Party Supports';2='Out-Party Supports'")
ggplot(forplot_comb,aes(x=fake,y=coef1))+geom_line()+facet_grid(battery~CRT)+xlab("Party ID Strength")+theme_bw()+ylab("Marginal effect of In-party Cue on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub),alpha=.5, fill='darkgreen', colour="black")+geom_hline(yintercept = 0,lty="dashed")

#save results
ggsave("TTIP_CRT_NFC_simple.pdf",width=8,height=3)


### Appendix H.4 Analysis of Frame Complexity------------------
m5<-lm(supportttip~partyidentity+simplecomplex+partyidentity*simplecomplex,data)
m6<-lm(supportttip~partyidentity+simplecomplex+zero1(CRTall)+ partyidentity*simplecomplex + simplecomplex*zero1(CRTall) +partyidentity*simplecomplex*zero1(CRTall) ,data)
m7<-lm(supportttip~partyidentity+simplecomplex+nfc+ partyidentity*simplecomplex + simplecomplex*nfc +partyidentity*simplecomplex*nfc ,data)
m8<-lm(supportttip~partyidentity+simplecomplex+cogresources+ partyidentity*simplecomplex + simplecomplex*cogresources +partyidentity*simplecomplex*cogresources ,data)

names(m7$coefficients)[names(m7$coefficients) == "nfc"] <- "zero1(CRTall)"
names(m7$coefficients)[names(m7$coefficients) == "simplecomplexSimple:nfc"] <- "simplecomplexSimple:zero1(CRTall)"
names(m7$coefficients)[names(m7$coefficients) == "partyidentity:nfc"] <- "partyidentity:zero1(CRTall)"
names(m7$coefficients)[names(m7$coefficients) == "partyidentity:simplecomplexSimple:nfc"] <- "partyidentity:simplecomplexSimple:zero1(CRTall)"
names(m8$coefficients)[names(m8$coefficients) == "cogresources"] <- "zero1(CRTall)"
names(m8$coefficients)[names(m8$coefficients) == "simplecomplexSimple:cogresources"] <- "simplecomplexSimple:zero1(CRTall)"
names(m8$coefficients)[names(m8$coefficients) == "partyidentity:cogresources"] <- "partyidentity:zero1(CRTall)"
names(m8$coefficients)[names(m8$coefficients) == "partyidentity:simplecomplexSimple:cogresources"] <- "partyidentity:simplecomplexSimple:zero1(CRTall)"


stargazer(m5, m6, m7, m8, title="Trade Policy Experiment: Analysis of Complexity Manipulation", align=TRUE,  covariate.labels=c("Partisan Social Identity Strength (PID)", "Complex (vs. simple)", "Cognitive resources", "PID*Complexity", "Complexity*Cog resources","PID*Cog resources", "PID*Cog resources*Complexity" ), omit.stat=c("LL","ser","f", "adj.rsq"),  notes.append = FALSE, column.labels=c("PID","CRT", "NfC", "Cognitive resources"),  star.cutoffs=c(0.05),notes = "*p<0.05", no.space=TRUE,out = "Complexity.tex",dep.var.caption = "Policy support",dep.var.labels.include = F,label="tab:complexity", digits=2)

## CRT at 0 or -1SD
m0 <- lm(supportttip~simplecomplex*scale(CRTall,center=0,scale=F)*partyidentity,data)
summary(m0)
m_0 <- interplot(m0,var1 = "simplecomplex",var2='partyidentity')
m_0$data$CRT='-1 SD'

## CRT at mean
m1 <- lm(supportttip~simplecomplex*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity,data)
m_1 <- interplot(m2,var1 = "simplecomplex",var2='partyidentity')
m_1$data$CRT='Mean'

## CRT at +1
m2 <- lm(supportttip~simplecomplex*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity,data)
summary(m2)
m_2 <- interplot(m2,var1 = "simplecomplex",var2='partyidentity')
m_2$data$CRT='+1 SD'

forplot <- rbind(m_0$data,m_1$data,m_2$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

## NFC at -1SD
m0_nfc <- lm(supportttip~simplecomplex*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity,data)
summary(m0_nfc)
m_0_nfc <- interplot(m0_nfc,var1 = "simplecomplex",var2='partyidentity')
m_0_nfc$data$CRT='-1 SD'

## CRT at mean
m1_nfc <- lm(supportttip~simplecomplex*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity,data)
summary(m1_nfc)
m_1_nfc <- interplot(m1_nfc,var1 = "simplecomplex",var2='partyidentity')
m_1_nfc$data$CRT='Mean'

## CRT at +1
m2_nfc <- lm(supportttip~simplecomplex*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity,data)
summary(m2_nfc)
m_2_nfc <- interplot(m2_nfc,var1 = "simplecomplex",var2='partyidentity')
m_2_nfc$data$CRT='+1 SD'

forplot_nfc <- rbind(m_0_nfc$data,m_1_nfc$data,m_2_nfc$data)
forplot_nfc$battery      <- c("NfC")

cogresources
## NFC at -1SD
m0_nfc <- lm(supportttip~simplecomplex*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity,data)
summary(m0_nfc)
m_0_nfc <- interplot(m0_nfc,var1 = "simplecomplex",var2='partyidentity')
m_0_nfc$data$CRT='-1 SD'

## CRT at mean
m1_nfc <- lm(supportttip~simplecomplex*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity,data)
summary(m1_nfc)
m_1_nfc <- interplot(m1_nfc,var1 = "simplecomplex",var2='partyidentity')
m_1_nfc$data$CRT='Mean'

## CRT at +1
m2_nfc <- lm(supportttip~simplecomplex*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity,data)
summary(m2_nfc)
m_2_nfc <- interplot(m2_nfc,var1 = "simplecomplex",var2='partyidentity')
m_2_nfc$data$CRT='+1 SD'

forplot_cog <- rbind(m_0_nfc$data,m_1_nfc$data,m_2_nfc$data)
forplot_cog$battery      <- c("Cogntive\n resources")

#combine CRT and NFC results
forplot_comb <- rbind(forplot, forplot_nfc, forplot_cog)
forplot_comb$CRT <- factor(forplot_comb$CRT,levels = c("-1 SD","Mean","+1 SD"))
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NfC","Cogntive\n resources"))

ggplot(forplot_comb,aes(x=fake,y=coef1))+geom_line()+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Complex-framing on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub),alpha=.5, fill='darkgreen', colour="black")+geom_hline(yintercept = 0,lty="dashed")

#save results
ggsave("TTIP_complexity.pdf",width=8,height=6)

### Appendix H.5 Inspection of non-linearity--------------
data$partyidentity_cat1<-0
data$partyidentity_cat1[data$partyidentity>.291 & data$partyidentity<.542]=1
data$partyidentity_cat2<-0
data$partyidentity_cat2[data$partyidentity>=.542]=1

## CRT at 0 or -1SD
m0 <- lm(supportttip~ownpartysupport*scale(CRTall,center=0,scale=F)*partyidentity_cat1+ ownpartysupport*scale(CRTall,center=0,scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m0)
m_0_mod <- interplot(m0,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_0_mod$data$CRT='-1 SD'
m_0_high <- interplot(m0,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_0_high$data$CRT='-1 SD'

## CRT at mean
m1 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity_cat1+ownpartysupport*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1_mod <- interplot(m1,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_1_mod$data$CRT='Mean'
m_1_high <- interplot(m1,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_1_high$data$CRT='Mean'

## CRT at +1
m2 <- lm(supportttip~ownpartysupport*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity_cat1+ownpartysupport*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_mod <- interplot(m2,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_2_mod$data$CRT='+1 SD'
m_2_high <- interplot(m2,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_2_high$data$CRT='+1 SD'

forplot <- rbind(m_0_mod$data,m_0_high$data, m_1_mod$data,m_1_high$data, m_2_mod$data,m_2_high$data)

#remove lines that are not necessary
forplot <- forplot[-c(3, 7,11), ] 
forplot$fake<-rep(1:3,3)
forplot$fake <- factor(forplot$fake,levels = c("1","2","3"))
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#NFC
## NFC at 0 or -1SD
m0_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)-sd(nfc))*partyidentity_cat1+ ownpartysupport*scale(nfc,center=mean(nfc)-sd(nfc))*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_0_mod_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_0_mod_nfc$data$CRT='-1 SD'
m_0_high_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_0_high_nfc$data$CRT='-1 SD'
## NfC at mean
m1_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity_cat1+ownpartysupport*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1_mod_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_1_mod_nfc$data$CRT='Mean'
m_1_high_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_1_high_nfc$data$CRT='Mean'
## NfC at +1
m2_nfc <- lm(supportttip~ownpartysupport*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity_cat1+ownpartysupport*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_mod_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_2_mod_nfc$data$CRT='+1 SD'
m_2_high_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_2_high_nfc$data$CRT='+1 SD'
forplot_nfc <- rbind(m_0_mod_nfc$data,m_0_high_nfc$data, m_1_mod_nfc$data,m_1_high_nfc$data, m_2_mod_nfc$data,m_2_high_nfc$data)

#remove lines that are not necessary
forplot_nfc <- forplot_nfc[-c(3, 7,11), ] 
forplot_nfc$fake<-rep(1:3,3)
forplot_nfc$fake <- factor(forplot_nfc$fake,levels = c("1","2","3"))
forplot_nfc$battery      <- c("NFC")
forplot_nfc$CRT <- factor(forplot_nfc$CRT,levels = c("-1 SD","Mean","+1 SD"))

#Cog
## NFC at 0 or -1SD
m0_nfc <- lm(supportttip~ownpartysupport*scale(cogresources,center=mean(cogresources)-sd(cogresources))*partyidentity_cat1+ ownpartysupport*scale(cogresources,center=mean(cogresources)-sd(cogresources))*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m0)
m_0_mod_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_0_mod_nfc$data$CRT='-1 SD'
m_0_high_nfc <- interplot(m0_nfc,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_0_high_nfc$data$CRT='-1 SD'

## NfC at mean
m1_nfc <- lm(supportttip~ownpartysupport*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity_cat1+ownpartysupport*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1_mod_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_1_mod_nfc$data$CRT='Mean'
m_1_high_nfc <- interplot(m1_nfc,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_1_high_nfc$data$CRT='Mean'

## NfC at +1
m2_nfc <- lm(supportttip~ownpartysupport*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity_cat1+ownpartysupport*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity_cat2+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_mod_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity_cat1')
m_2_mod_nfc$data$CRT='+1 SD'
m_2_high_nfc <- interplot(m2_nfc,var1 = "ownpartysupport",var2='partyidentity_cat2')
m_2_high_nfc$data$CRT='+1 SD'

forplot_cog<- rbind(m_0_mod_nfc$data,m_0_high_nfc$data, m_1_mod_nfc$data,m_1_high_nfc$data, m_2_mod_nfc$data,m_2_high_nfc$data)

forplot_cog <- forplot_cog[-c(3, 7,11), ] 
forplot_cog$fake<-rep(1:3,3)
forplot_cog$fake <- factor(forplot_cog$fake,levels = c("1","2","3"))
forplot_cog$battery      <- c("Cognitive\n resources")
forplot_cog$CRT <- factor(forplot_cog$CRT,levels = c("-1 SD","Mean","+1 SD"))

#combine CRT and NFC results
forplot_comb <- rbind(forplot, forplot_nfc, forplot_cog)
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NFC","Cognitive\n resources"))
ggplot(forplot_comb,aes(x=fake,y=coef1))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of In-party Cue on Policy Support ")+geom_pointrange(aes(ymin=lb,ymax=ub),alpha=.5, colour="black")+geom_hline(yintercept = 0,lty="dashed")+scale_x_discrete(labels = c("1" = "Low","2" = "Modest", "3"="Strong"))+ theme(strip.text.y = element_text(angle = 360))

#save results
ggsave("TTIP_nonlinear.pdf",width=8,height=6)